package com.TIEmulator.FileIOLib;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager.LayoutParams;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class InternetFileIO
{
  public static final int DOWNLOAD_ERROR = 1;
  public static final int DOWNLOAD_SUCCESS = 0;
  private static final int MAX_BUFFER_SIZE = 1024;

  protected static int download(Handler paramHandler, String paramString1, String paramString2)
  {
    Log.d("InternetFileIO.download", "destFileName: <" + paramString1 + "> inetUrl: <" + paramString2 + ">");
    URL localURL;
    try
    {
      localURL = new URL(paramString2);
    }
    catch (MalformedURLException localMalformedURLException)
    {
      int i;
      label76: int i1;
      int i2;
      label187: label236: int i4;
      try
      {
        URLConnection localURLConnection = localURL.openConnection();
        localURLConnection.setReadTimeout(60000);
        int j = localURLConnection.getContentLength();
        if (j < 1)
          for (i = 1; ; i = 1)
          {
            return i;
            localMalformedURLException = localMalformedURLException;
            localMalformedURLException.printStackTrace();
          }
        Log.d("InternetFileIO.download", "open inet url : download size: " + j + " byte");
        InputStream localInputStream = localURLConnection.getInputStream();
        BufferedInputStream localBufferedInputStream = new BufferedInputStream(localInputStream);
        Log.d("InternetFileIO.download", "BufferedInputStream created!");
        BufferedOutputStream localBufferedOutputStream = new BufferedOutputStream(new FileOutputStream(paramString1));
        Log.d("InternetFileIO.download", "open destination file!");
        int k = j / 20;
        int l = 1;
        i1 = 0;
        i2 = 0;
        int i3 = j - i2;
        if (i3 > 1024);
        for (byte[] arrayOfByte = new byte[1024]; ; arrayOfByte = new byte[i3])
        {
          i1 = localBufferedInputStream.read(arrayOfByte);
          if (i1 != -1)
            break;
          Log.d("InternetFileIO.download", "EOF REACHED!");
          Log.d("InternetFileIO.download", "in and output now is closed, file sucessfully written!");
          localBufferedOutputStream.flush();
          localBufferedOutputStream.close();
          localBufferedInputStream.close();
          localInputStream.close();
          i = 0;
          break label76:
          if (i3 <= 0)
            break label367;
        }
        localBufferedOutputStream.write(arrayOfByte, 0, i1);
        i4 = i2 + i1;
        if ((paramHandler == null) || (i4 <= k * l))
          break label360;
        Message localMessage = new Message();
        localMessage.arg1 = 0;
        paramHandler.sendMessage(localMessage);
        ++l;
        label360: label367: i2 = i4;
      }
      catch (Exception localException)
      {
        localException.printStackTrace();
        i = 1;
        break label76:
        i2 = i4;
        break label187:
        break label236:
      }
    }
  }

  public static int download(String paramString1, String paramString2)
  {
    return download(null, paramString1, paramString2);
  }

  public static void startDownloadGUIThread(Context paramContext, String paramString1, String paramString2)
  {
    new DownloadThread(paramContext, paramString1, paramString2);
  }

  private static class DownloadThread extends Thread
  {
    private static final int DISMISS_DOWNLOAD_DLG = 2;
    private static final int SHOW_DOWNLOAD_DLG = 1;
    public static final int UPDATE_DOWNLOAD_DLG;
    private ProgressDialog downLoadingDialog = null;
    private String down_from_url;
    private String down_to_dir;
    private AlertDialog failureDialog = null;
    private final Handler handler = new Handler()
    {
      public void handleMessage(Message paramMessage)
      {
        switch (paramMessage.arg1)
        {
        default:
        case 1:
        case 0:
        case 2:
        }
        while (true)
        {
          return;
          InternetFileIO.DownloadThread.this.downLoadingDialog.show();
          continue;
          InternetFileIO.DownloadThread.this.downLoadingDialog.incrementProgressBy(5);
          continue;
          InternetFileIO.DownloadThread.this.downLoadingDialog.dismiss();
          if (paramMessage.arg2 == 0)
            InternetFileIO.DownloadThread.this.successDialog.show();
          InternetFileIO.DownloadThread.this.failureDialog.show();
        }
      }
    };
    private AlertDialog successDialog = null;

    public DownloadThread(Context paramContext, String paramString1, String paramString2)
    {
      WindowManager.LayoutParams localLayoutParams = new WindowManager.LayoutParams();
      localLayoutParams.flags = 2;
      localLayoutParams.windowAnimations = 17432577;
      this.downLoadingDialog = new ProgressDialog(paramContext);
      this.downLoadingDialog.setMessage(paramContext.getString(2131165210));
      this.downLoadingDialog.getWindow().setAttributes(localLayoutParams);
      this.downLoadingDialog.setProgressStyle(1);
      this.downLoadingDialog.setMax(100);
      this.downLoadingDialog.setProgress(0);
      this.downLoadingDialog.setCancelable(false);
      this.successDialog = new AlertDialog.Builder(paramContext).setIcon(17301659).setMessage(2131165238).setNeutralButton(17039370, null).create();
      String str = paramContext.getString(2131165237);
      this.failureDialog = new AlertDialog.Builder(paramContext).setTitle(str).setIcon(17301659).setMessage(paramContext.getString(2131165236)).create();
      this.down_to_dir = paramString1;
      this.down_from_url = paramString2;
      Message localMessage = new Message();
      localMessage.arg1 = 1;
      start();
      this.handler.sendMessage(localMessage);
    }

    public void run()
    {
      Message localMessage = new Message();
      localMessage.arg1 = 2;
      localMessage.arg2 = InternetFileIO.download(this.handler, this.down_to_dir, this.down_from_url);
      this.handler.sendMessage(localMessage);
    }
  }
}
